package mapper;

import com.sqsf.javabean.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
import org.apache.ibatis.mapping.FetchType;

public interface UserMapper {
    List<User> selectAll();


    @Select("select * from user where username=#{username} and password=#{password}")
    User selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password);
    //mybatis注解可以简化mapper的配置 甚至可以不书写mapper

    @Select("select * from user where uid=#{uid}")
    User selectByUid(int uid);

    @Insert("insert into user (username,password,phone)values(#{username},#{password},#{phone})")
    int insertUser(User user);

    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);

    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid") int uid,@Param("password") String password);


    @Select("select u.*,r.* from user u left join user_role ur on u.uid=ur.uid left join role r on ur.rid=r.rid")
    @Results(id = "u_r",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "rid",property = "r.rid"),
            @Result(column = "name",property = "r.name"),
            @Result(column = "desc",property = "r.desc")
    })
    List<User> selectUser_Role();

    @Select("select * from user")
    @Results(id = "u_r1",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(property = "r",column = "uid",one=@One(
                    select = "mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<User> selectUser_Role1();
}
